package com.hk.huakangcompany.controller;

import com.github.pagehelper.PageInfo;
import com.hk.huakangcompany.commons.security.CommonResult;
import com.hk.huakangcompany.model.CompanyMessage;
import com.hk.huakangcompany.model.vo.Result;
import com.hk.huakangcompany.model.vo.ResultVO;
import com.hk.huakangcompany.service.CompanyMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/3 13:26
 * @Statement: 公司信息Controller层
 */
@RestController
@RequestMapping("/company")
public class CompanyController {

//    private Jedis jedis = JedisUtil.getJedis();

    @Autowired
    private CompanyMessageService iCompanyMessageService;
    @GetMapping("/findAll")
    public ResultVO<CompanyMessage> findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10")Integer limit,
            CompanyMessage companyMessage
    ) {
        List<CompanyMessage> companyMessageList = iCompanyMessageService.findAll(page, limit, companyMessage);
        PageInfo<CompanyMessage> companyMessagePageInfo = new PageInfo<>(companyMessageList);
        int count = iCompanyMessageService.count(-1,-1, companyMessage);
        if (companyMessageList.isEmpty()) {
            ResultVO resultVO = CommonResult.nullResultVO(count,companyMessagePageInfo.getList());
            return resultVO;
        }
        ResultVO resultVO = CommonResult.successResultVO(count,companyMessagePageInfo.getList());
        return resultVO;
//        return null;
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id) {
        CompanyMessage companyMessage = iCompanyMessageService.findById(id);
        if (companyMessage.getId() == 0) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        Result result = CommonResult.successResult(companyMessage);
        return result;
//        return null;
    }

    @PostMapping("/save")
    public Result save(@RequestBody(required = false) CompanyMessage companyMessage) {
        if (companyMessage == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        //        清楚Redis中的公司信息
//        jedis.del("company");
        iCompanyMessageService.save(companyMessage);
        //        清楚Redis中的公司信息
//        jedis.del("company");
        Result result = CommonResult.successResult(null);
        return result;
//        return null;
    }

    @PostMapping("/updateMessage")
    public Result updateMessage(@RequestBody(required = false) CompanyMessage companyMessage) throws IOException {
        if (companyMessage == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        //        清楚Redis中的公司信息
//        jedis.del("company");
        iCompanyMessageService.update(companyMessage);
        //        清楚Redis中的公司信息
//        jedis.del("company");
        Result result = CommonResult.successResult(null);
        return result;
    }

    @PostMapping("/updateState")
    public Result updateState(@RequestBody(required = false) CompanyMessage companyMessage) {
        if (companyMessage == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        //        清楚Redis中的公司信息
//        jedis.del("company");
        iCompanyMessageService.updateState(companyMessage);
        //        清楚Redis中的公司信息
//        jedis.del("company");
        Result result = CommonResult.successResult(null);
        return result;
    }

    @PostMapping("/deletetCompany/{id}")
    public Result deletetCompany(@PathVariable int id) throws IOException {
        //        清楚Redis中的公司信息
//        jedis.del("company");
        iCompanyMessageService.delete(id);
        //        清楚Redis中的公司信息
//        jedis.del("company");
        Result result = CommonResult.successResult(null);
        return result;
    }




}
